#!/usr/bin/env bash
# Copyright (c) 2021 The Toltec Contributors
# SPDX-License-Identifier: MIT

archs=(rm1os2 rm2os2)
pkgnames=(ddvk-hacks)
pkgdesc="Enhance Xochitl with additional features"
url=https://github.com/ddvk/remarkable-hacks
pkgver=39.01-4
timestamp=2022-11-09T18:31:51Z
section="readers"
maintainer="Mattéo Delabre <spam@delab.re>"
license=MIT
flags=(nostrip)
conflicts=(webinterface-onboot signature-rm)

source=(https://github.com/ddvk/remarkable-hacks/archive/90e7e3e7ffc269373de191085453be50c9f8da0c.zip)
sha256sums=(d3b1413bb9219804581afab598e7f5308233e7467d64e8084e67aae7346beaba)

_patches_dir="/opt/share/ddvk-hacks"
_xochitl_path="/usr/bin/xochitl"
_work_dir="/home/root/.local/share/ddvk-hacks"
_info_path="$_work_dir/xochitl.info"
_backup_path="$_work_dir/xochitl.backup"
_old_backup_path="$_backup_path.old"
_patched_path="$_work_dir/xochitl.patched"

package() {
    mkdir -p "$pkgdir"/opt/share/toltec/reenable.d
    touch "$pkgdir"/opt/share/toltec/reenable.d/"$pkgname"
    if [[ $arch = rm1os2 ]]; then
        patches_dir="${pkgdir}${_patches_dir}"
        install -D -m 644 -t "$patches_dir" "$srcdir"/patches/26171_rm1/patch_19.1.02
        install -D -m 644 -t "$patches_dir" "$srcdir"/patches/26275_rm1/patch_20.1.03
        install -D -m 644 -t "$patches_dir" "$srcdir"/patches/27051_rm1/patch_21.1.04
        install -D -m 644 -t "$patches_dir" "$srcdir"/patches/28098_rm1/patch_23.1.02
        install -D -m 644 -t "$patches_dir" "$srcdir"/patches/291236_rm1/patch_24.1.02
        install -D -m 644 -t "$patches_dir" "$srcdir"/patches/2100324_rm1/patch_25.1.03
        install -D -m 644 -t "$patches_dir" "$srcdir"/patches/2102356_rm1/patch_27.1.03
        install -D -m 644 -t "$patches_dir" "$srcdir"/patches/2103379_rm1/patch_28.1.02
        install -D -m 644 -t "$patches_dir" "$srcdir"/patches/2110442_rm1/patch_29.1.02
        install -D -m 644 -t "$patches_dir" "$srcdir"/patches/2121527_rm1/patch_30.1.08
        install -D -m 644 -t "$patches_dir" "$srcdir"/patches/2122573_rm1/patch_31.1.01
        install -D -m 644 -t "$patches_dir" "$srcdir"/patches/2123606_rm1/patch_32.1.03
        install -D -m 644 -t "$patches_dir" "$srcdir"/patches/2140861_rm1/patch_34.1.01
        install -D -m 644 -t "$patches_dir" "$srcdir"/patches/2141866_rm1/patch_35.1.01
        install -D -m 644 -t "$patches_dir" "$srcdir"/patches/2143977_rm1/patch_36.1.01
        install -D -m 644 -t "$patches_dir" "$srcdir"/patches/21431047_rm1/patch_37.1.01
        install -D -m 644 -t "$patches_dir" "$srcdir"/patches/21501067_rm1/patch_38.1.03
        install -D -m 644 -t "$patches_dir" "$srcdir"/patches/21511189_rm1/patch_39.1.01
    elif [[ $arch = rm2os2 ]]; then
        patches_dir="${pkgdir}${_patches_dir}"
        install -D -m 644 -t "$patches_dir" "$srcdir"/patches/26171_rm2/patch_19.2.02
        install -D -m 644 -t "$patches_dir" "$srcdir"/patches/26275_rm2/patch_20.2.03
        install -D -m 644 -t "$patches_dir" "$srcdir"/patches/27051_rm2/patch_21.2.05
        install -D -m 644 -t "$patches_dir" "$srcdir"/patches/27153_rm2/patch_22.2.01
        install -D -m 644 -t "$patches_dir" "$srcdir"/patches/28098_rm2/patch_23.2.02
        install -D -m 644 -t "$patches_dir" "$srcdir"/patches/291217_rm2/patch_24.2.04
        install -D -m 644 -t "$patches_dir" "$srcdir"/patches/2100324_rm2/patch_25.2.01
        install -D -m 644 -t "$patches_dir" "$srcdir"/patches/2101332_rm2/patch_26.2.02
        install -D -m 644 -t "$patches_dir" "$srcdir"/patches/2102356_rm2/patch_27.2.05
        install -D -m 644 -t "$patches_dir" "$srcdir"/patches/2103379_rm2/patch_28.2.02
        install -D -m 644 -t "$patches_dir" "$srcdir"/patches/2110442_rm2/patch_29.2.02
        install -D -m 644 -t "$patches_dir" "$srcdir"/patches/2121527_rm2/patch_30.2.07
        install -D -m 644 -t "$patches_dir" "$srcdir"/patches/2122573_rm2/patch_31.2.01
        install -D -m 644 -t "$patches_dir" "$srcdir"/patches/2123606_rm2/patch_32.2.02
        install -D -m 644 -t "$patches_dir" "$srcdir"/patches/2130758_rm2/patch_33.2.01
        install -D -m 644 -t "$patches_dir" "$srcdir"/patches/2140861_rm2/patch_34.2.01
        install -D -m 644 -t "$patches_dir" "$srcdir"/patches/2141866_rm2/patch_35.2.01
        install -D -m 644 -t "$patches_dir" "$srcdir"/patches/2143977_rm2/patch_36.2.01
        install -D -m 644 -t "$patches_dir" "$srcdir"/patches/21431047_rm2/patch_37.2.01
        install -D -m 644 -t "$patches_dir" "$srcdir"/patches/21501067_rm2/patch_38.2.03
        install -D -m 644 -t "$patches_dir" "$srcdir"/patches/21511189_rm2/patch_39.2.01
    fi
}

configure() {
    local build_date
    build_date="$(cat /etc/version)"
    local patch_version
    local device
    local original_hash
    local xochitl_version
    if [[ $arch = rm1os2 ]]; then
        device="reMarkable 1"
        case "$build_date" in
            "20221026103859")
                patch_version="39.1.01"
                original_hash="1c01bae469a2e29846c68758e2cae4a2b8b5055d"
                xochitl_version="2.15.1.1189"
                ;;
            "20221003074737")
                patch_version="38.1.03"
                original_hash="a3ce408c8a717d48746e361336532924f5ff40f2"
                xochitl_version="2.15.0.1067"
                ;;
            "20220921102803")
                patch_version="37.1.01"
                original_hash="011742f027b70f37a0da293132daafcfc82b537d"
                xochitl_version="2.14.3.1047"
                ;;
            "20220825122914")
                patch_version="36.1.01"
                original_hash="a88faec812ae20960bfbab38be0aa49aafec902a"
                xochitl_version="2.14.3.977"
                ;;
            "20220617142418")
                patch_version="35.1.01"
                original_hash="a052dfbe587851d17146e586e4be65819f1360e3"
                xochitl_version="2.14.1.866"
                ;;
            "20220615075543")
                patch_version="34.1.01"
                original_hash="a3da3cc4d393917703bcdc2ed3c280ebcc192e0f"
                xochitl_version="2.14.0.861"
                ;;
            "20220330140034")
                patch_version="32.1.03"
                original_hash="e6693c76ad588c7a223cf5be5a280214495a68e0"
                xochitl_version="2.12.3.606"
                ;;
            "20220303122245")
                patch_version="31.1.01"
                original_hash="00b5095022a48e48cdbe435b8afff0e3dc736b96"
                xochitl_version="2.12.2.573"
                ;;
            "20220202133055")
                patch_version="30.1.08"
                original_hash="c542ee591b45cb18599dc852cc0d3ce82ec86b56"
                xochitl_version="2.12.1.527"
                ;;
            "20211208080907")
                patch_version="29.1.02"
                original_hash="1f72eb42b0745d40196cb7fece6a8fee55f958c0"
                xochitl_version="2.11.0.442"
                ;;
            "20211102142308")
                patch_version="28.1.02"
                original_hash="1978c56c0bf9a53e74fb05b7212381543adb709e"
                xochitl_version="2.10.3.379"
                ;;
            "20211014150444")
                patch_version="27.1.03"
                original_hash="a76509107656c03e866ec169ad317da9111f71be"
                xochitl_version="2.10.2.356"
                ;;
            "20210923152158")
                patch_version="25.1.03"
                original_hash="635917603b0d9349ddf5f6ead818701a42979945"
                xochitl_version="2.10.0.324"
                ;;
            "20210820111232")
                patch_version="24.1.02"
                original_hash="e5d3ce51e81a6e23bac0c66549b809e6dd4e35a0"
                xochitl_version="2.9.1.236"
                ;;
            "20210611154039")
                patch_version="23.1.02"
                original_hash="b688b4afbd5c13347bada20f26b9108d82658f9a"
                xochitl_version="2.8.0.98"
                ;;
            "20210504114855")
                patch_version="21.1.04"
                original_hash="123877bb7dd6133f4540e03d3912b1d4c4d76050"
                xochitl_version="2.7.0.51"
                ;;
            "20210322075617")
                patch_version="20.1.03"
                original_hash="50a44683ac1b8ce524e55e10f57b699e7c4ca409"
                xochitl_version="2.6.2.75"
                ;;
            "20210311193614")
                patch_version="19.1.02"
                original_hash="2d31db3e7f1a7b98a493cc5a1351fe303f849cd3"
                xochitl_version="2.6.1.71"
                ;;
            *)
                echo
                echo "Error: The version the device is running is not supported, yet."
                echo "Build date: $build_date"
                echo
                exit 1
                ;;
        esac
    elif [[ $arch = rm2os2 ]]; then
        device="reMarkable 2"
        case "$build_date" in
            "20221026104022")
                patch_version="39.2.01"
                original_hash="64e3cb3d05aec4a40624ebfc730e480358e1b184"
                xochitl_version="2.15.1.1189"
                ;;
            "20221003075633")
                patch_version="38.2.03"
                original_hash="fcd3c84215c5457d455419831760304736f3b694"
                xochitl_version="2.15.0.1067"
                ;;
            "20220921101206")
                patch_version="37.2.01"
                original_hash="47c3ad26651f604be5de901881a8a44f9124a79c"
                xochitl_version="2.14.3.1047"
                ;;
            "20220825124750")
                patch_version="36.2.01"
                original_hash="470e88f8d5fb62f64939fe4ea3b89c515113f7e5"
                xochitl_version="2.14.3.977"
                ;;
            "20220617143306")
                patch_version="35.2.01"
                original_hash="1d7f6f049e5a6b192caaf07cbf67ba7c2555e5f0"
                xochitl_version="2.14.1.866"
                ;;
            "20220615074909")
                patch_version="34.2.01"
                original_hash="6376fb20bdf6952bb7b75838c0bda1c80f0e9191"
                xochitl_version="2.14.0.861"
                ;;
            "20220519120030")
                patch_version="33.2.01"
                original_hash="c77016e4608ccab1b1e619a6ef2769a205312025"
                xochitl_version="2.13.0.758"
                ;;
            "20220330134519")
                patch_version="32.2.02"
                original_hash="b7d8f0ca786117cdf715e3f7b08b1eac3aed907a"
                xochitl_version="2.12.3.606"
                ;;
            "20220303120824")
                patch_version="31.2.01"
                original_hash="7105e311df4df7f4ccde457ce574da63f49d3a4e"
                xochitl_version="2.12.2.573"
                ;;
            "20220202133838")
                patch_version="30.2.07"
                original_hash="8728cf8a2677a1b458f8e1ed665f8c1358568f7f"
                xochitl_version="2.12.1.527"
                ;;
            "20211208075454")
                patch_version="29.2.02"
                original_hash="ad88de508a3c7da7f1ff6a9d394806c5d987026d"
                xochitl_version="2.11.0.442"
                ;;
            "20211102143141")
                patch_version="28.2.02"
                original_hash="8510d6f4380b6155d630baecaccccfb0147263d0"
                xochitl_version="2.10.3.379"
                ;;
            "20211014151303")
                patch_version="27.2.05"
                original_hash="2cc077e0bc5eca53664d4692197d54b477fa02ba"
                xochitl_version="2.10.2.356"
                ;;
            "20210929140057")
                patch_version="26.2.02"
                original_hash="44ed43a128c821988519c3ea92c4516f011edd7e"
                xochitl_version="2.10.1.332"
                ;;
            "20210923144714")
                patch_version="25.2.01"
                original_hash="0990b599d412a1c6368985d872383b29b47ffab6"
                xochitl_version="2.10.0.324"
                ;;
            "20210812195523")
                patch_version="24.2.04"
                original_hash="ab37e201e819e4212fb4e2ce92cd747614499fbb"
                xochitl_version="2.9.1.217"
                ;;
            "20210611153600")
                patch_version="23.2.02"
                original_hash="02a851bc33231fcd253eee781eb127e3c942da69"
                xochitl_version="2.8.0.98"
                ;;
            "20210511153632")
                patch_version="22.2.01"
                original_hash="f0846772da9b810aecc2a307ba5dae21c072674a"
                xochitl_version="2.7.1.53"
                ;;
            "20210504114631")
                patch_version="21.2.05"
                original_hash="f0846772da9b810aecc2a307ba5dae21c072674a"
                xochitl_version="2.7.0.51"
                ;;
            "20210322075357")
                patch_version="20.2.03"
                original_hash="7b314d6fb03c8789396f0ed43a4b27a18c649d2d"
                xochitl_version="2.6.2.75"
                ;;
            "20210311194323")
                patch_version="19.2.02"
                original_hash="fc434bf45f1ff927af799ddccc7b2b0449f516f7"
                xochitl_version="2.6.1.71"
                ;;
            *)
                echo
                echo "Error: The version the device is running is not supported, yet."
                echo "Build date: $build_date"
                echo
                exit 1
                ;;
        esac
    else
        echo
        echo "Error: The architecture the device is running is not supported, yet."
        echo "Build date: $build_date"
        echo
        exit 1
    fi

    echo
    echo "Device: $device"
    echo "Xochitl version: $xochitl_version"
    echo "Patch version: $patch_version"
    echo

    if ! sha1sum -c <(echo "$original_hash  $_xochitl_path") > /dev/null 2>&1; then
        echo "Error: Invalid Xochitl checksum"
        echo "Maybe ddvk-hacks are already installed?"
        echo
        exit 1
    fi

    if [[ -f "$_backup_path" ]]; then
        mv "$_backup_path" "$_old_backup_path"
    fi

    mkdir -p "$(dirname "$_backup_path")"
    cp "$_xochitl_path" "$_backup_path"

    echo "Patching Xochitl"
    bspatch "$_backup_path" "$_patched_path" "$_patches_dir"/patch_"$patch_version"
    cp "$_patched_path" "$_xochitl_path"
    rm -rf /home/root/.cache/remarkable/xochitl/qmlcache/*

    # shellcheck source=../toltec-bootstrap/toltecctl
    source /home/root/.local/bin/toltecctl
    get-release-version > "$_info_path"

    echo "Please restart Xochitl to use the patches"
    echo
}

_restore() {
    # shellcheck source=../toltec-bootstrap/toltecctl
    source /home/root/.local/bin/toltecctl

    if ! [ -f "$_info_path" ] \
        || [[ "$(get-release-version)" != "$(cat "$_info_path")" ]] \
        || compare-versions "$(get-release-version)" "3.0"; then
        echo "Skipping restore, as you are on a newer OS version"
        return
    fi

    echo
    echo "Restoring the original Xochitl binary"

    if ! diff "$_xochitl_path" "$_patched_path" > /dev/null 2>&1; then
        echo "Warning: Xochitl binary has changed!"
        echo "Not restoring the backup"
    else
        cp "$_backup_path" "$_xochitl_path"
        rm -rf /home/root/.cache/remarkable/xochitl/qmlcache/*
        rm "$_info_path"
    fi

    echo
}

preremove() {
    _restore
}

preupgrade() {
    _restore
}
